package com.whsw.scheduler.trading_center_scheduler.scheduler.reportFormScheduler;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
/**
 * 建设工程交易招标项目明细（公开）定时器
 * @Author liangyiqiang 2021/03/20
 * */
@Component
public class BidProjectsForConstructionProjectsDetailsScheduler {

    @Autowired
    DSFactory dsFactory;

    /**
     * 统计报表-建设工程交易招标项目明细（公开）
     * @Author liangyiqiang 2021/03/20
     * 广联达尚未答复以下数据在数据库是什么类型
     * b.budget_amount '总投资和e.predicted_contract_price '预计发包价
     * 使用市区财政性投资是否超过50%，是否资格审查，是否电子评标项目
     *
     * 还差额外字段：
     * 项或投资计划批准文号名称、
     * 立项或投资计划批准文号地区所属名称、
     * 立项或投资计划批准文号地址、
     * 规划报建批准文号、
     * 初步设计批准文号、
     * 建设用地批准文号、
     * 招标项目规模定性描述
     * */
    @Scheduled(cron = "0/10 * * * * ?")
    public void getBidProjectsForConstructionProjectsDetailsInfo() throws Exception {
        Db db1 = Db.use(dsFactory.getDataSource("a"));
        Db db2 = Db.use(dsFactory.getDataSource("b"));//我们数据库字段是double类型，对方的数据是decimal类型
        List<Entity> list = db2.query("" +
                "select" +
                "b.tender_name AS bid_unit," +
                "b.supervise_dept_name AS project_supervision_dept,\n" +
                "b.budget_amount AS total_investment," +
                "e.predicted_contract_price AS expect_offer_price," +
                "b.tender_content AS tender_content,\n" +
                "b.industry AS project_type," +
                "b.tender_category AS project_classify_name," +
                "b.tender_way AS bid_method_name," +
                "b.project_address AS tender_project_loacation," +
                "d.price AS bid_price," +
                "d.enterprise_name AS bid_enterprise,\n" +
                "case when b.city_percent > 50  then 1 else 0 end AS fiscal_flag,\n" +
                "b.city_percent AS urban_investment," +
                "b.district_percent AS region_investment," +
                "b.province_percent AS province_investment," +
                "b.collective_percent AS collective_investment"+
                "b.collective_percent AS state_owned_investment," +
                "b.private_percent AS private_investment," +
                "b.foreign_capital_percent AS foreign_investment,\n" +
                "case b.electrization when 'paper' then 0 else 0 end AS bid_assessment_flag," +
                "case b.qualification_type when 'zgys' then 1 else 0 end AS review_flag \n" +
                "from pub_t_package a\n" +
                "left join pub_t_project b on a.project_id = b.project_id \n" +
                "left join pub_t_result_letter c on c.package_id = a.package_id \n" +
                "left join gb_t_result_letter_enterprise d on d.result_letter_id = c.result_letter_id \n" +
                "left join gb_t_project_extend e on e.project_id = b.project_id \n" +
                "where \n" +
                "c.approval_status = 'passed'\n" +
                "and c.status = 1");

        if(!ObjectUtils.isEmpty(list)){
            db1.del(
                    Entity.create("bid_projects_for_construction_projects_details")
            );
            for (Entity entity : list) {
                entity.setTableName("bid_projects_for_construction_projects_details");
            }
            db1.insert(list);
        }
    }
}
